﻿using System;
using System.Data;
using System.Windows.Forms;
using ChinaDynastyTours.Controls;
using ChinaDynastyTours.Helpers;
using ChinaDynastyTours.Models;
using System.Collections.Generic;

namespace ChinaDynastyTours.Views
{
    public partial class GuidePlanForm : DevExpress.XtraEditors.XtraForm
    {
        //输入参数
        public string _TourGroupID { get; set; }//团ID
        //public string _LocalPlanScheduleDate { get; set; }//团状态
        //public string _LocalPlanScheduleID { get; set; }//行程计划ID
        //public string _TourGroupScheduleID { get; set; }//大程计划ID
        //public string _LoginProviderID { get; set; }
        public DateTime _TourgroupStartDate { get; set; }
        public DateTime _TourgroupEndDate { get; set; }

        public DataTable _dtPlanData;

        public GuidePlanForm()
        {
            InitializeComponent();
        }
       
        private void RestaurantOrderForm_Load(object sender, EventArgs e)
        {
            this.ciceroniGridControl.DataSource = BaseInfoCtrl.GetCiceroniData(ApplicationHelper.CurrentLoginProviderNameAndID[1]);
            LoadPlanGridPageData();
        }

        private void LoadPlanGridPageData()
        {
            //获取导游安排信息
            _dtPlanData = GuideArrangeCtrl.GetGuideArrangeData(_TourGroupID, int.Parse(ApplicationHelper.CurrentLoginProviderNameAndID[1]));
            _dtPlanData.ColumnChanged += new DataColumnChangeEventHandler(GuidePlanData_ColumnChanged);
            guidePlanGridControl.DataSource = _dtPlanData; 
        }

        #region 修改预订信息后的自动保存

        void GuidePlanData_ColumnChanged(object sender, DataColumnChangeEventArgs e)
        {
            GuideArrangeEntity entity = new GuideArrangeEntity();
            entity.GuideArrangeID = int.Parse(e.Row["GuideArrangeID"].ToString());
            entity.Remark = e.Row["Remark"].ToString();
            entity.StartDate = e.Row["StartDate"].ToString();//UI中‘日期’和‘时间’绑定的都是一个字段，因此取一个就可以
            entity.EndDate = e.Row["EndDate"].ToString();
            entity.Notified = false;
            entity.IsAccompany = false;

            GuideArrangeCtrl.UpdateGuideArrangeData(entity);

        }
        #endregion
        
        private void btnAdd_Click(object sender, EventArgs e)
        {
            if (ciceroniGridView.DataRowCount == 0)
                return;
            List<GuideArrangeEntity> entities = new List<GuideArrangeEntity>();

            GuideArrangeEntity entity = new GuideArrangeEntity();
            entity.Calendars = "";
            entity.CiceroniName = ciceroniGridView.GetRowCellValue(ciceroniGridView.FocusedRowHandle, "CiceroniName").ToString();
            entity.CiceroniID = int.Parse(ciceroniGridView.GetRowCellValue(ciceroniGridView.FocusedRowHandle, "CiceroniID").ToString());
            entity.TourgroupID = _TourGroupID;
            entity.StartDate = _TourgroupStartDate.ToString("yyyy-MM-dd HH:mm");
            entity.EndDate = _TourgroupEndDate.ToString("yyyy-MM-dd HH:mm");
            entity.Notified = false;
            entity.IsAccompany = false;
            entity.HasEvent = 0;//0代表无事件,1代表事件信息，5代表紧急事件
            entity.Remark = "";// scheduleGridView.GetRowCellValue(scheduleGridView.FocusedRowHandle, "DestinationCity").ToString() + "上团";
            entity.OwnerProviderID = ApplicationHelper.CurrentLoginProviderNameAndID[1];
            entities.Add(entity);
            GuideArrangeCtrl.CreateGuideArrangeData(entities);

            LoadPlanGridPageData();
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            if (guidePlanGridView.FocusedRowHandle < 0) return;

            if (MessageBox.Show("确定移除选择的导游安排信息吗？", "提示信息", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
            {
                string guideArrangeId = guidePlanGridView.GetRowCellValue(guidePlanGridView.FocusedRowHandle, "GuideArrangeID").ToString();
                GuideArrangeCtrl.DeleteGuideArrangeData(guideArrangeId);
            }
            LoadPlanGridPageData();
        }

        #region 关闭窗口保存数据
        private void btnExit_Click(object sender, EventArgs e)
        {
            this.Close();
        }
 
        #endregion

        private void ciceroniGridView_RowClick(object sender, DevExpress.XtraGrid.Views.Grid.RowClickEventArgs e)
        {
            if (e.Clicks == 2)
            {
                btnAdd.PerformClick();
            }
        }

    }
}
